package ahpu.piconet.dao.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import ahpu.piconet.dao.DataDao;
import ahpu.piconet.domin.Data;

@Repository
public class DataDaoImpl implements DataDao {

	@Autowired
	private SessionFactory sessionFactory;

	private Session getSession() {
		return sessionFactory.openSession();
	}

	@Override
	public void addData(Data data) {
		this.getSession().save(data);
	}

	@Override
	public List<Data> findByDate(String start, String end, int page, int limit) {
		List<Data> datas = new ArrayList<Data>();
		String hql = "from Data d where d.timer > ? AND d.timer < ?";
		Query query = this.getSession().createQuery(hql);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date sTime = sdf.parse(start);
			Date eTiem = sdf.parse(end);
			System.out.println(sTime);
			query.setDate(0, sTime);
			query.setDate(1, eTiem);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		query.setFirstResult((page - 1) * limit);
		query.setMaxResults(limit);
		datas = query.list();
		return datas;
	}

	@Override
	public int findAllCount() {
		List<Data> datas = new ArrayList<Data>();
		String hql = "from Data";
		Query query = this.getSession().createQuery(hql);
		datas = query.list();
		return datas.size();
	}

	@Override
	public List<Data> findByLimit(int page, int limit) {
		List<Data> datas = new ArrayList<Data>();
		String hql = "from Data";
		Query query = this.getSession().createQuery(hql)
				.setFirstResult((page - 1) * limit).setMaxResults(limit);
		datas = query.list();
		return datas;
	}

	@Override
	public Data findById(Integer id) {
		String hql = "from Data d where d.id=?";
		this.getSession().createQuery(hql).setInteger(0, id).uniqueResult();
		return (Data) this.getSession().createQuery(hql).setInteger(0, id)
				.uniqueResult();
	}

	@Override
	public int findCountByDate(String start, String end) {
		List<Data> datas = new ArrayList<Data>();
		String hql = "from Data d where d.timer > ? AND d.timer < ?";
		Query query = this.getSession().createQuery(hql);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date sTime = sdf.parse(start);
			Date eTiem = sdf.parse(end);
			System.out.println(sTime);
			query.setDate(0, sTime);
			query.setDate(1, eTiem);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		datas = query.list();
		int a = datas.size();
		return a;
	}

	@Override
	public List<Data> queryExportedForestry(String[] ids) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < ids.length; i++) {
			sb.append(ids[i] + ",");
		}
		Query query = getSession().createQuery(
				"from Data where id in (" + sb.deleteCharAt(sb.toString().length() - 1).toString() + ")");
		return query.list();
	}
}
